package com.hankol.material.fragment

import android.app.Activity
import android.os.Bundle
import android.view.View
import androidx.viewpager.widget.ViewPager
import com.baselibrary.MessageBus
import com.baselibrary.activity.BaseFragment
import com.google.android.material.appbar.AppBarLayout
import com.hankol.material.R
import com.hankol.material.adapter.ContentListPagerAdapter
import com.hankol.material.adapter.MaterialTabAdapter
import com.hankol.material.bean.*
import com.hankol.material.databinding.FragmentMaterialContentListBinding
import com.hankol.material.viewmodel.MaterialBaseViewModel
import com.hankol.material.viewmodel.MaterialContentViewModel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.*

/**
 * @author: 关忆北丶
 * @time: 2021/10/18 13:22
 * @description: 图片/视频/文档 外层布局
 * @param bean 业务请求接口数据bean
 */
class MaterialControlContentFragment(private var bean: MaterialBusinessBean, private var appBar: AppBarLayout?, private var isFromCollect: Boolean = false, var activity: Activity) : BaseFragment() {
    private var onNumberListener: OnNumberListener? = null
    private lateinit var mAdapter: MaterialTabAdapter
    private lateinit var mContentListPagerAdapter: ContentListPagerAdapter
    private lateinit var mViewModel: MaterialContentViewModel
    private val mFragments = ArrayList<ContentListFragment>()
    private val mList = ArrayList<MaterialTabBean>()
    private lateinit var dataBinding: FragmentMaterialContentListBinding
    private var mContentInfoBean: MaterialContentInfo? = null
    var onMaterialLoadCompleteListener: MaterialBaseViewModel.OnMaterialLoadCompleteListener? = null//搜索

    companion object {
        fun newInstance(bean: MaterialBusinessBean, appBar: AppBarLayout?, isFromCollect: Boolean = false, activity: Activity): MaterialControlContentFragment {
            return MaterialControlContentFragment(bean, appBar, isFromCollect, activity)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_material_content_list
    }


    override fun init() {
        dataBinding = getDataBinding<FragmentMaterialContentListBinding>()
        mViewModel = getFragmentViewModelProvider(this).get(MaterialContentViewModel::class.java)
        mAdapter = MaterialTabAdapter()
        dataBinding.rvCatalogueType.adapter = mAdapter

        mContentListPagerAdapter = ContentListPagerAdapter(childFragmentManager)
        dataBinding.vpMaterial.adapter = mContentListPagerAdapter

        dataBinding.vpMaterial.offscreenPageLimit = 3
        handlerData(bean)

        mViewModel.contentInfoList.observe(this, androidx.lifecycle.Observer {
            hideLoadingDialog()
            if (it == null) return@Observer
            mContentInfoBean = it
            mList.clear()
            mFragments.clear()
            if (it.picCount > 0) {
                mList.add(MaterialTabBean("图片 ${it.picCount}", false, TabConfigure.IMG.tabConfig))
                mFragments.add(ContentListFragment.newInstance(TabConfigure.IMG.tabConfig, bean, onNumberListener, appBar, isFromCollect, activity))
            }
            if (it.videoCount > 0) {
                mList.add(MaterialTabBean("视频 ${it.videoCount}", false, TabConfigure.VIDEO.tabConfig))
                mFragments.add(ContentListFragment.newInstance(TabConfigure.VIDEO.tabConfig, bean, onNumberListener, appBar, isFromCollect, activity))
            }
            if (it.fileCount > 0) {
                mList.add(MaterialTabBean("文档 ${it.fileCount}", false, TabConfigure.FILE.tabConfig))
                mFragments.add(ContentListFragment.newInstance(TabConfigure.FILE.tabConfig, bean, onNumberListener, appBar, isFromCollect, activity))
            }
            if (mList.size > 0) {
                dataBinding.llData.visibility = View.VISIBLE
                dataBinding.tvEmpty.visibility = View.GONE
                mList[0].isSelected = true
                mAdapter.newSelected = -1
                mAdapter.setNewData(mList)
                mContentListPagerAdapter.setFragments(mFragments)
                dataBinding.vpMaterial.setCurrentItem(0, true)
            } else {
                mViewModel.setEmpty(bean, dataBinding.llData, dataBinding.tvEmpty)
            }
            onMaterialLoadCompleteListener?.onMaterialLoadComplete()
        })


        mViewModel.tabInfoList.observe(this, androidx.lifecycle.Observer {
            if (it == null) return@Observer
            val listPosition1 = mViewModel.getListPosition(mList, TabConfigure.IMG.tabConfig)
            if (listPosition1 != -1) {
                //说明该条目存在
                mList[listPosition1].title = "图片 ${it.picCount}"

            }
            val listPosition2 = mViewModel.getListPosition(mList, TabConfigure.VIDEO.tabConfig)
            if (listPosition2 != -1) {
                mList[listPosition2].title = "视频 ${it.videoCount}"
            }

            val listPosition3 = mViewModel.getListPosition(mList, TabConfigure.FILE.tabConfig)
            if (listPosition3 != -1) {
                mList[listPosition3].title = "文档 ${it.fileCount}"
            }
            mAdapter.setNewData(mList)
        })



        initListener()
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
    }

    fun handlerData(bean: MaterialBusinessBean) {
        showLoadingDialog()
        this.bean = bean
        mViewModel.handlerData(bean)
    }

    private fun initListener() {
        dataBinding.vpMaterial.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(i: Int, v: Float, i1: Int) {

            }

            override fun onPageSelected(i: Int) {
                mAdapter.upSelected(i)
            }

            override fun onPageScrollStateChanged(i: Int) {

            }
        })
        mAdapter.setOnItemClickListener { _, _, position ->
            dataBinding.vpMaterial.setCurrentItem(position, true)
            mAdapter.upSelected(position)
        }
        onNumberListener = object : OnNumberListener {
            override fun onNumberListener(type: Int, bean: FolderDetailInfoBean) {

            }
        }
    }


    //EventBus 接受方法
    @Subscribe(threadMode = ThreadMode.MAIN) fun onEvent(messageBus: MessageBus) {
        if (messageBus.codeType == MessageBus.msgId_material_moveCollection) {
            mContentInfoBean?.let {
                when (messageBus.param1 as Int) {
                    TabConfigure.IMG.tabConfig -> {
                        val listPosition = mViewModel.getListPosition(mList, TabConfigure.IMG.tabConfig)
                        if (listPosition != -1) {
                            //说明该条目存在
                            it.picCount = it.picCount - 1
                            mList[listPosition].title = "图片 ${it.picCount}"

                        }
                    }

                    TabConfigure.VIDEO.tabConfig -> {
                        val listPosition = mViewModel.getListPosition(mList, TabConfigure.VIDEO.tabConfig)
                        if (listPosition != -1) {
                            it.videoCount = it.videoCount - 1
                            mList[listPosition].title = "视频 ${it.videoCount}"
                        }
                    }

                    TabConfigure.FILE.tabConfig -> {
                        val listPosition = mViewModel.getListPosition(mList, TabConfigure.FILE.tabConfig)
                        if (listPosition != -1) {
                            it.fileCount = it.fileCount - 1
                            mList[listPosition].title = "文档 ${it.fileCount}"
                        }
                    }
                }
                mAdapter.setNewData(mList)
            }
        } else if (messageBus.codeType == MessageBus.msgid_tab_refresh) {
            mViewModel.handlerTab(bean)
        }
    }


    interface OnNumberListener {
        fun onNumberListener(type: Int, bean: FolderDetailInfoBean)
    }


    override fun onDestroy() {
        super.onDestroy()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }


    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
    }

}
